#include <material/glslsyntaxhighlighter.h>

GLSLSyntaxHighlighter::GLSLSyntaxHighlighter(QTextDocument* document)
  : QSyntaxHighlighter(document)
{
  _variables << "bool"
             << "float"
             << "double"
             << "int"
             << "uint"
             << "bvec2"
             << "bvec3"
             << "bvec4"
             << "ivec2"
             << "ivec3"
             << "ivec4"
             << "uvec2"
             << "uvec3"
             << "uvec4"
             << "vec2"
             << "vec3"
             << "vec4"
             << "mat2"
             << "mat3"
             << "mat4"
             << "centroid"
             << "patch"
             << "sample"
             << "dvec2"
             << "dvec3"
             << "dvec4"
             << "dmat2"
             << "dmat3"
             << "dmat4"
             << "mat2x2"
             << "mat2x3"
             << "mat2x4"
             << "mat3x2"
             << "mat3x3"
             << "mat3x4"
             << "mat4x2"
             << "mat4x3"
             << "mat4x4"
             << "dmat2x2"
             << "dmat2x3"
             << "dmat2x4"
             << "dmat3x2"
             << "dmat3x3"
             << "dmat3x4"
             << "dmat4x2"
             << "dmat4x3"
             << "dmat4x4"
             << "sampler1D"
             << "sampler2D"
             << "sampler3D"
             << "samplercube"
             << "sampler1Dshadow"
             << "sampler2Dshadow"
             << "samplercubeshadow"
             << "sampler1Darray"
             << "sampler2Darray"
             << "sampler1Darrayshadow"
             << "sampler2Darrayshadow"
             << "isampler1D"
             << "isampler2D"
             << "isampler3D"
             << "isamplercube"
             << "isampler1Darray"
             << "isampler2Darray"
             << "usampler1D"
             << "usampler2D"
             << "usampler3D"
             << "usamplercube"
             << "usampler1Darray"
             << "usampler2Darray"
             << "sampler2Drect"
             << "sampler2Drectshadow"
             << "isampler2Drect"
             << "usampler2Drect"
             << "samplerbuffer"
             << "isamplerbuffer"
             << "usamplerbuffer"
             << "samplercubearray"
             << "samplercubearrayshadow"
             << "isamplercubearray"
             << "usamplercubearray"
             << "sampler2Dms"
             << "isampler2Dms"
             << "usampler2Dms"
             << "sampler2Dmsarray"
             << "isampler2Dmsarray"
             << "usampler2Dmsarray"
             << "void";

  _keywords << "const"
            << "break"
            << "continue"
            << "do"
            << "else"
            << "for"
            << "if"
            << "discard"
            << "return"
            << "switch"
            << "case"
            << "default"
            << "subroutine"
            << "in"
            << "out"
            << "inout"
            << "uniform"
						<< "varying"
						<< "attribute"
            << "noperspective"
            << "flat"
            << "smooth"
            << "layout"
            << "struct"
            << "while";

  _keywordFormat.setFontWeight(QFont::Bold);
  _keywordFormat.setForeground(Qt::darkRed);

  _variableFormat = QTextCharFormat(_keywordFormat);
  _variableFormat.setFontWeight(QFont::Bold);
  _variableFormat.setForeground(Qt::darkBlue);

  _versionFormat = QTextCharFormat(_keywordFormat);
  _versionFormat.setFontWeight (QFont::Normal);
  _versionFormat.setForeground(Qt::darkGray);

	_commentFormat = QTextCharFormat(_keywordFormat);
	_commentFormat.setFontItalic(true);
	_commentFormat.setForeground(Qt::darkGreen);
}


void GLSLSyntaxHighlighter::highlightBlock(const QString &text)
{

  foreach(QString variable, _variables)
    {
      QString pattern = "\\b" + variable +"\\b";

      QRegExp expression(pattern);
      int index = text.indexOf(expression);
      while (index >= 0)
        {
          int length = expression.matchedLength();
          setFormat(index, length, _variableFormat);
          index = text.indexOf(expression, index + length);
        }

    }

  foreach(QString keyword, _keywords)
    {
      QString pattern = "\\b" + keyword +"\\b";

      QRegExp expression(pattern);
      int index = text.indexOf(expression);
      while (index >= 0)
        {
          int length = expression.matchedLength();
          setFormat(index, length, _keywordFormat);
          index = text.indexOf(expression, index + length);
        }
    }

	QString pattern = "\\/\\/";

	QRegExp expression(pattern);
	int index = text.indexOf(expression);
	if (index >= 0)
		{
			int length = text.size() - index;
			setFormat(index, length, _commentFormat);
		}

  pattern = QRegExp::escape("#");
	expression = QRegExp (pattern);
	index = text.indexOf(expression);
	if (index >= 0)
		{
			int length = text.size() - index;
			setFormat(index, length, _versionFormat);
		}


}
